//
//  SCAudioVisualViewController.m
//  OurMoon
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "SCAudioVisualViewController.h"
#import "NightHeader.h"
#import "HTTPMethod.h"
#import "SCAudioVisualTableView.h"
#import "SCHeaderCollectionView.h"
#import "VideoListModel.h"
#import "VideoSidListModel.h"
#import "SCVideoPlayViewController.h"
#import "SCHeaderViewController.h"
#import "SCRadioModel.h"
#import "SCRadioSectionModel.h"
#import "SCRadioPlayViewController.h"
#import "SCRadioMoreViewController.h"
#import "SCRecentPlayViewController.h"
#import <MBProgressHUD.h>
#import <MJRefresh.h>
#import "Music.h"
#import "SCRadioPlayViewController.h"
#import "GiFHUD.h"
#import "FitHeader.h"
@interface SCAudioVisualViewController ()<UIScrollViewDelegate, HeaderViewControllerByVideoDelegate, HeaderViewControllerByRadioDelegate,VideoPlayViewControllerDelegate, RadioPlayViewControllerDelegate, RadioMoreViewControllerDelegate>
@property(nonatomic, strong)UISegmentedControl *segmentedControl;
@property(nonatomic, strong)UIScrollView *bottomScrollView; //底部滚动scrollw
@property(nonatomic, strong)SCAudioVisualTableView *videoTableView; //视频
@property(nonatomic, strong)SCHeaderCollectionView *videoHeaderCollectionView;//视频headerView
@property(nonatomic, strong)SCAudioVisualTableView *radioTableView; //电台
@property(nonatomic, strong)SCHeaderCollectionView *radioHeaderCollectionView;//电台headerView
@property(nonatomic, strong)NSMutableArray *dataVideoListArray;
@property(nonatomic, strong)NSMutableArray *dataVideoSidListArray;
@property(nonatomic, strong)NSMutableArray *dataRadioListArray;
@property(nonatomic, strong)NSMutableArray *dataRadioSectionArray;
@property(nonatomic, strong)MBProgressHUD *progressView;
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, strong)UIImageView *dongImageView;
@end

@implementation SCAudioVisualViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *nameArray = @[@"视频", @"电台"];
    _segmentedControl = [[UISegmentedControl alloc] initWithItems:nameArray];
    _segmentedControl.frame = CGRectMake(100 * FitWidth, 0, 200 * FitWidth, 30 * FitHeight);
    //修改边框颜色
    _segmentedControl.tintColor = [UIColor whiteColor];
    //点击是否显示选中状态
    _segmentedControl.momentary = NO;

    _segmentedControl.selectedSegmentIndex = 0;
    //_segmentedControl.layer.masksToBounds = YES;
    //_segmentedControl.layer.cornerRadius = 10;
    self.navigationItem.titleView = _segmentedControl;
    [_segmentedControl addTarget:self action:@selector(exchangePage:) forControlEvents:UIControlEventValueChanged];
    
    
    [self.view NightWithType:UIViewColorTypeNormal];
   
   
//    _progressView = [[MBProgressHUD alloc] initWithView:self.view];
//    _progressView.labelText = @"加载中";
//    [_progressView setMode:MBProgressHUDModeIndeterminate];
//    [_progressView show:YES];
//    _progressView.backgroundColor = [UIColor colorWithRed:0.62 green:0.65 blue:0.66 alpha:1];
//    _progressView.alpha = 1;
//   [self.view addSubview:_progressView];
    self.dataVideoListArray = [NSMutableArray array];
    self.dataVideoSidListArray = [NSMutableArray array];
    _page = 0;
    _videoTableView.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //先将原来数据清空
        [_dataVideoListArray removeAllObjects];
        [_dataVideoSidListArray removeAllObjects];
        //将页码重新设置为1
        _page = 0;
        [self getVideoData];//再次请求数据
    }];
    //上拉加载
    _videoTableView.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++; //页码加+10;
        NSLog(@"%ld", _page);
        [self getVideoData];
    }];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    _dongImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 32 * FitWidth, 32 * FitHeight)];
    
    _dongImageView.image = [UIImage imageNamed:@"c1.png"];
    
   self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_dongImageView];
    
    _dongImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(turnToPlayView)];
    [_dongImageView addGestureRecognizer:tap];
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isPlaying"]) {
        [self tapAngry];
    }
    if ([Music shareInstance].flag == 1) {
         [self tapAngry];
    }
    
    //[[NSUserDefaults standardUserDefaults]addObserver:self forKeyPath:@"isPlaying" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

}
-(void)turnToPlayView
{
  
    if ([Music shareInstance].flag == 1) {
        [[Music shareInstance].player pause];
        [_dongImageView stopAnimating];
        [Music shareInstance].flag = 0;
    }
    else{
        [[Music shareInstance].player play];
        [Music shareInstance].flag = 1;
        [_dongImageView startAnimating];
    }

//    PlayViewController *playVC = [[PlayViewController alloc]init];
//    [self.navigationController pushViewController:playVC animated:YES];
//    [self.navigationController pushViewController:[SCRadioPlayViewController shareInstance] animated:YES];
}

-(void)tapAngry
{
    NSMutableArray  *array = [NSMutableArray array];
    NSString *nameString = @"";
    for (NSInteger i = 1; i <= 11; i++) {
        nameString = [NSString stringWithFormat:@"c%ld.png", (long)i];
        UIImage *image = [UIImage imageNamed:nameString];
        [array addObject:image];
    }
    _dongImageView.animationImages = array;
    _dongImageView.animationDuration = 0.7;
    [_dongImageView startAnimating];
    
}



- (void)exchangePage:(UISegmentedControl *)seg
{
    if (seg.selectedSegmentIndex == 0) {
        _bottomScrollView.contentOffset = CGPointMake(0, 0);
    }
    
    if (seg.selectedSegmentIndex == 1) {
        _bottomScrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    }
}
#pragma mark 滚动就会触发
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_bottomScrollView.contentOffset.x == 0) {
        _segmentedControl.selectedSegmentIndex = 0;
    }
    if (_bottomScrollView.contentOffset.x == self.view.frame.size.width) {
        _segmentedControl.selectedSegmentIndex = 1;
    }
}

- (void)loadView
{
    [super loadView];
    [self getVideoData];
    [self getRadioData];
    //加载页面
    [GiFHUD setGifWithImageName:@"loading.gif"];
    //初始化底部scrollw
    _bottomScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49)];
    _bottomScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height - 64 - 49);
    _bottomScrollView.contentOffset = CGPointMake(0, 0);
    _bottomScrollView.showsHorizontalScrollIndicator = NO; //横向
    _bottomScrollView.pagingEnabled = YES;
    _bottomScrollView.delegate = self;
    [_bottomScrollView NightWithType:UIViewColorTypeNormal];
    [self.view addSubview:_bottomScrollView];
    
    //初始化视频
    _videoTableView = [[SCAudioVisualTableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) WithType:SCTableViewByVideo];
    _videoTableView.delegate = self;
    [_videoTableView NightWithType:UIViewColorTypeNormal];
    [_bottomScrollView addSubview:_videoTableView];
    //初始化视频headerView
    _videoTableView.tableView.tableHeaderView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 97 * FitHeight)];

    //初始化HeaderCollectionView
   _videoHeaderCollectionView = [[SCHeaderCollectionView alloc] initWithFrame:_videoTableView.tableView.tableHeaderView.frame WithType:SCCollectionViewFlowLayoutByVideo];
    [_videoHeaderCollectionView NightWithType:UIViewColorTypeNormal];
   _videoHeaderCollectionView.delegate = self;
    //为视频headerView赋值
   _videoTableView.tableView.tableHeaderView = _videoHeaderCollectionView;

    //初始化电台
    _radioTableView = [[SCAudioVisualTableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) WithType:SCTableViewByRadio];
    _radioTableView.delegateRadio = self;
    _radioTableView.delegateMoreRadio = self;
    [_radioTableView NightWithType:UIViewColorTypeNormal];
    [_bottomScrollView addSubview:_radioTableView];
    //初始化电台headerView
    _radioTableView.tableView.tableHeaderView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45 * FitHeight)];
    //初始化HeaderCollectionView
    _radioHeaderCollectionView = [[SCHeaderCollectionView alloc] initWithFrame:_radioTableView.tableView.tableHeaderView.frame WithType:SCCollectionViewFlowLayoutByRadio];
    [_radioHeaderCollectionView NightWithType:UIViewColorTypeNormal];
    _radioHeaderCollectionView.delegateRadio = self;
    //为视频headerView赋值
    _radioTableView.tableView.tableHeaderView = _radioHeaderCollectionView;
    
    
    //电台跳转播放页面的方法
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JumpRadioPlay:) name:@"jump" object:nil];
    
}
#pragma mark - 获取视频数据
- (void)getVideoData
{
     [GiFHUD show];
    [HTTPMethod getDataByString:[NSString stringWithFormat:@"http://c.m.163.com/nc/video/home/%ld-10.html", _page * 10] BodyDic:nil WithDataBlock:^(id data) {
        for (NSDictionary *videoListdic in [data objectForKey:@"videoList"]) {
            VideoListModel *videoListModel = [[VideoListModel alloc] init];
            [videoListModel setValuesForKeysWithDictionary:videoListdic];
            [_dataVideoListArray addObject:videoListModel];
        }
        [_dataVideoSidListArray removeAllObjects];
        for (NSDictionary *videoSidListDic in [data objectForKey:@"videoSidList"]) {
            VideoSidListModel *videoSidListModel = [[VideoSidListModel alloc] init];
            [videoSidListModel setValuesForKeysWithDictionary:videoSidListDic];
            
            [_dataVideoSidListArray addObject:videoSidListModel];
        }
        //视频
        _videoHeaderCollectionView.dataVideoSidListArray = _dataVideoSidListArray;
        _videoTableView.dataVideoListArray = _dataVideoListArray;
        [_videoTableView.tableView reloadData];
        [GiFHUD dismiss];
        //电台
         _radioHeaderCollectionView.dataVideoListArray = [NSMutableArray arrayWithObjects:@"我的下载", @"最近播放", nil];
        
       // [_progressView removeFromSuperview];
        [_videoTableView.tableView.header endRefreshing];
        [_videoTableView.tableView.footer endRefreshing];
        
    }];
}

#pragma mark - 获取电台数据
- (void)getRadioData
{
     self.dataRadioListArray = [NSMutableArray array];
    self.dataRadioSectionArray = [NSMutableArray array];
    [HTTPMethod getDataByString:@"http://c.m.163.com/nc/topicset/android/radio/index.html" BodyDic:nil WithDataBlock:^(id data) {
        for (NSDictionary *dic in [data objectForKey:@"cList"]) {
            //
            SCRadioSectionModel *radioSectionModel = [[SCRadioSectionModel alloc] init];
            radioSectionModel.cid = [dic objectForKey:@"cid"];
            radioSectionModel.cname = [dic objectForKey:@"cname"];
            [_dataRadioListArray addObject:radioSectionModel];
            //
            NSMutableArray *array = [NSMutableArray array];
            SCRadioModel *radioModel = nil;
            for (NSDictionary *tListDic in [dic objectForKey:@"tList"]) {
                NSDictionary *radioDic = [NSDictionary dictionaryWithDictionary:[tListDic objectForKey:@"radio"]];
                radioModel = [[SCRadioModel alloc] init];
                [radioModel setValuesForKeysWithDictionary:radioDic];
                radioModel.tid = [tListDic objectForKey:@"tid"];
                [array addObject:radioModel];
            }
            [_dataRadioListArray addObject:array];
        }
       // _radioTableView.dataRadioSectionArray = _dataRadioSectionArray;
        _radioTableView.dataRadioListArray = _dataRadioListArray;
       // [_progressView removeFromSuperview];
        
    }];
}

#pragma mark - 视频头部视图跳转详情页面代理方法
- (void)HeaderViewControllerByVideoJump:(VideoSidListModel *)videoSidListModel
{
    SCHeaderViewController *hcv = [[SCHeaderViewController alloc] init];
    hcv.videoSidListModel = videoSidListModel;
    [self.navigationController pushViewController:hcv animated:YES];
}
#pragma mark - 电台头部视图跳转详情页面代理方法
- (void)HeaderViewControllerByRadioJump:(NSString *)string
{
    SCRecentPlayViewController *rpc = [[SCRecentPlayViewController alloc] init];
    rpc.flagStr = string;
    [self.navigationController pushViewController:rpc animated:YES];
}
#pragma mark - 视频跳转视频播放页面的代理方法
- (void)VideoPlayViewControllerJump:(VideoListModel *)videoListModel
{
    SCVideoPlayViewController *vpvc = [[SCVideoPlayViewController alloc] init];
    vpvc.videoListModel = videoListModel;
    [self.navigationController pushViewController:vpvc animated:YES];
}
#pragma mark - 电台跳转播放页面的通知中心方法
- (void)JumpRadioPlay:(NSNotification *)notification
{
    SCRadioModel *radioListModel = (SCRadioModel *)[notification object];
    SCRadioPlayViewController *rvc = [[SCRadioPlayViewController alloc] init];
    rvc.radioModel = radioListModel;
    [self.navigationController pushViewController:rvc animated:YES];
}
#pragma mark - 电台跳转每个分类更多页面的代理方法
- (void)RadioMoreViewControllerJump:(SCRadioSectionModel *)radioSectionModel
{
    SCRadioMoreViewController *rmc = [[SCRadioMoreViewController alloc] init];
    rmc.radioSectionModel = radioSectionModel;
    [self.navigationController pushViewController:rmc  animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
